package com.duowei.diandan.fragment.dialog;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.adapter.TicketAdapter;
import com.duowei.diandan.bean.Money;
import com.duowei.diandan.bean.Ticket;
import com.duowei.diandan.databinding.TicketDialogFragmentBinding;
import com.duowei.diandan.impl.OnTicketClickListener;

import java.util.ArrayList;

public class TicketDialogFragment extends BaseDialogFragment {

    private TicketDialogFragmentBinding mBinding;
    private ArrayList<Ticket> mTicketList;
    private Money mMoney;

    public static TicketDialogFragment newInstance(ArrayList<Ticket> ticketList, Money money) {

        Bundle args = new Bundle();
        args.putSerializable("ticketList", ticketList);
        args.putSerializable("money", money);

        TicketDialogFragment fragment = new TicketDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            //noinspection unchecked
            mTicketList = (ArrayList<Ticket>) getArguments().getSerializable("ticketList");
            mMoney = (Money) getArguments().getSerializable("money");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_ticket_dialog, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        TicketAdapter ticketAdapter = new TicketAdapter(mTicketList);
        ticketAdapter.setOnTicketClickListener(ticket -> {
            dismiss();

            if (mOnTicketClickListener != null) {
                mOnTicketClickListener.onTicketClick(ticket);
            }
        });
        mBinding.ticketRecyclerView.setAdapter(ticketAdapter);

        mBinding.closeButton.setOnClickListener(v -> dismiss());
    }

    private OnTicketClickListener mOnTicketClickListener;

    public void setOnTicketClickListener(OnTicketClickListener onTicketClickListener) {
        mOnTicketClickListener = onTicketClickListener;
    }
}
